package cn.idietrade.idietradegoodsorder8003.demo.service.impl;

import cn.idietrade.common.pojo.OrderGoods;
import cn.idietrade.common.pojo.SecondOrder;
import cn.idietrade.idietradegoodsorder8003.demo.mapper.OrderGoodsMapper;
import cn.idietrade.idietradegoodsorder8003.demo.service.OrderGoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zjy
 * @version 1.0
 * @createTime 2022-12-25 14:30
 * @description
 */
@Service
public class OrderGoodsServiceImpl implements OrderGoodsService {
    @Resource
    private OrderGoodsMapper orderGoodsMapper;


    @Override
    public int addOrderGoods(Integer orderId,Integer userId,String goodsIds,String nums) {
        List<OrderGoods> orderList =new ArrayList<>();

        //拆分两个字符串
        List<Integer> goodsIdList= new ArrayList<>();
        String[] splitgoodsId = goodsIds.split(",");
        for (String s : splitgoodsId) {
            goodsIdList.add(Integer.parseInt(s));
        }

        List<Integer> numsList= new ArrayList<>();
        String[] splitnum = nums.split(",");
        for (String s : splitnum) {
            numsList.add(Integer.parseInt(s));
        }

        OrderGoods orderGoods = null;
        for (int i = 0; i <goodsIdList.size() ; i++) {
            orderGoods=new OrderGoods();
            orderGoods.setOrderId(orderId);
            orderGoods.setUserId(userId);
            orderGoods.setGoodsId(goodsIdList.get(i));
            orderGoods.setNum(numsList.get(i));
            orderList.add(orderGoods);
        }


        int flag = 0;
        for (OrderGoods goods : orderList) {
           flag+=orderGoodsMapper.addOrderGoods(goods);
        }

        if(flag>0){
            return 1;
        }
        return 0;
    }
}
